import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.regex.Pattern;

public class ChangeCustomerPasswordFrame extends JFrame {

    private JPasswordField oldPasswordField;
    private JPasswordField newPasswordField;
    private JPasswordField confirmPasswordField;

    private Connection connection;

    public ChangeCustomerPasswordFrame() {
        setTitle("修改客户密码");
        setSize(350, 200);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(4, 2));
        add(panel);

        JLabel oldPasswordLabel = new JLabel("旧密码:");
        oldPasswordField = new JPasswordField();
        JLabel newPasswordLabel = new JLabel("新密码:");
        newPasswordField = new JPasswordField();
        JLabel confirmPasswordLabel = new JLabel("确认新密码:");
        confirmPasswordField = new JPasswordField();
        JButton changeButton = new JButton("修改密码");
        JButton cancelButton = new JButton("取消");

        panel.add(oldPasswordLabel);
        panel.add(oldPasswordField);
        panel.add(newPasswordLabel);
        panel.add(newPasswordField);
        panel.add(confirmPasswordLabel);
        panel.add(confirmPasswordField);
        panel.add(changeButton);
        panel.add(cancelButton);

        changeButton.addActionListener(this::handleChangeButtonClick);
        cancelButton.addActionListener(e -> dispose());

        connectToDatabase();
    }

    private void connectToDatabase() {
        try {
            // 数据库连接设置
            String url = "jdbc:mysql://localhost:3306/sql_shopping";
            String user = "root";
            String password = "123456";
            connection = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this, "无法连接到数据库！");
            System.exit(1);
        }
    }

    private void handleChangeButtonClick(ActionEvent e) {
        String oldPassword = new String(oldPasswordField.getPassword());
        String newPassword = new String(newPasswordField.getPassword());
        String confirmPassword = new String(confirmPasswordField.getPassword());

        if (validatePassword(newPassword)) {
            if (newPassword.equals(confirmPassword)) {
                if (changeCustomerPassword(oldPassword, newPassword)) {
                    JOptionPane.showMessageDialog(this, "密码修改成功！");
                    dispose();
                } else {
                    JOptionPane.showMessageDialog(this, "旧密码错误或修改失败！");
                }
            } else {
                JOptionPane.showMessageDialog(this, "新密码与确认密码不匹配！");
            }
        } else {
            JOptionPane.showMessageDialog(this, "新密码不符合要求！(密码长度大于8个字符，必须是大小写字母、数字和标点符号的组合)");
        }
    }

    private boolean validatePassword(String password) {
        if (password.length() <= 8) {
            return false;
        }
        boolean hasUpper = !password.equals(password.toLowerCase());
        boolean hasLower = !password.equals(password.toUpperCase());
        boolean hasDigit = password.chars().anyMatch(Character::isDigit);
        boolean hasSpecial = Pattern.compile("[!@#$%^&*(),.?\":{}|<>]").matcher(password).find();

        return hasUpper && hasLower && hasDigit && hasSpecial;
    }

    private boolean changeCustomerPassword(String oldPassword, String newPassword) {
        try {
            // 验证旧密码
            String checkPasswordQuery = "SELECT * FROM customers WHERE password = ?";
            PreparedStatement checkPasswordStmt = connection.prepareStatement(checkPasswordQuery);
            checkPasswordStmt.setString(1, oldPassword);
            ResultSet rs = checkPasswordStmt.executeQuery();

            if (rs.next()) {
                // 更新新密码
                String updatePasswordQuery = "UPDATE customers SET password = ? WHERE password = ?";
                PreparedStatement updatePasswordStmt = connection.prepareStatement(updatePasswordQuery);
                updatePasswordStmt.setString(1, newPassword);
                updatePasswordStmt.setString(2, oldPassword);
                int rowsUpdated = updatePasswordStmt.executeUpdate();
                return rowsUpdated > 0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

}
